<template>
  <common-list>
    <common-search slot="search" @search="handleQuery" @reset="resetQuery">
      <el-form
        ref="queryForm"
        :inline="true"
        :model="queryParams"
        v-show="showSearch"
        @submit.native.prevent
        label-width="65px"
      >
        <el-form-item label="系统名称" prop="businessName">
          <common-input
            :verify="false"
            v-model="queryParams.businessName"
            @enter="handleQuery"
            clearable
          />
        </el-form-item>
        <el-form-item label="等保定级" prop="protectLevel">
          <el-select v-model="queryParams.protectLevel" filterable>
            <el-option label="全部" :value="null"></el-option>
            <el-option
              v-for="item in protectLevelList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" filterable>
            <el-option label="全部" :value="null"></el-option>
            <el-option label="启用" :value="0"></el-option>
            <el-option label="停用" :value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </common-search>
    <el-row :gutter="10" slot="tools">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          @click="handleAdd"
          v-hasPermi="['system:businessSystem:add']"
          >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          type="primary"
          icon="el-icon-download"
          @click="importShow"
          v-hasPermi="['system:businessSystem:importBusinessSystem']"
          >导入
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          type="danger"
          icon="el-icon-delete"
          :disabled="!showBatch"
          v-hasPermi="['system:businessSystem:remove']"
          @click="handleDelete()"
          >批量删除
        </el-button>
      </el-col>
    </el-row>

    <common-table
      slot="content"
      v-loading="loading"
      :data="list"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="系统编码"
        align="left"
        show-overflow-tooltip
        prop="businessCode"
        fixed="left"
      />
      <el-table-column
        label="系统名称"
        align="left"
        prop="businessName"
        show-overflow-tooltip
      />
      <el-table-column
        label="等保定级"
        align="left"
        show-overflow-tooltip
        width="200"
      >
        <template slot-scope="scope">
          {{ getProtectLevelLabel(scope.row.protectLevel) }}
        </template>
      </el-table-column>
      <el-table-column
        label="关键信息基础设施"
        align="left"
        prop="businessCode"
        show-overflow-tooltip
        width="200"
      >
        <template slot-scope="scope">
          {{ scope.row.isFacilities === 0 ? "否" : "是" }}
        </template>
      </el-table-column>
      <el-table-column
        label="负责人"
        align="left"
        prop="userCode"
        show-overflow-tooltip
      />
      <el-table-column
        label="简介"
        align="left"
        prop="introduce"
        width="250"
        show-overflow-tooltip
      />
      <el-table-column
        label="创建时间"
        align="left"
        prop="createTime"
        width="170"
        show-overflow-tooltip
      />
      <el-table-column label="状态" align="center" width="80px">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.status === 0">启用 </el-tag>
          <el-tag type="danger" v-else-if="scope.row.status === 1"
            >停用
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="left" width="140" fixed="right">
        <template slot-scope="scope">
          <!-- <el-button type="text" @click="handleDetail(scope.row)"
            >详情
          </el-button> -->
          <el-button
            type="text"
            @click="handleShake(scope.row)"
            :disabled="scope.row.isDisabledForShake"
            v-hasPermi="['system:businessSystem:updateStatus']"
          >
            {{ scope.row.status === 0 ? "停用" : "启用" }}
          </el-button>
          <el-button
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:businessSystem:edit']"
          >
            编辑
          </el-button>
          <el-button
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:businessSystem:remove']"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </common-table>

    <pagination
      slot="pagination"
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <Add ref="systemAdd" @refresh="getList"></Add>
    <Detail ref="systemDetail"></Detail>

    <common-upload-dialog
      download-path="system/businessSystem/downFileByTemplateName"
      upload-path="/system/businessSystem/importData"
      template-name="businessSystemTemplate.xls"
      report-path="system/downErrorReportByemplateName"
      ref="uploadDialog"
      :getList="getList"
    />
  </common-list>
</template>

<script>
import BaseList from "@/mixins/BaseList";
import Add from "./components/add.vue";
import Detail from "./components/detail.vue";
import {
  list,
  remove,
  updateStatus,
} from "@/api/system/businessSystem/businessSystem";

const protectLevelList = [
  {
    label: "未通过网络安全等级保护测评",
    value: 0,
  },
  { label: "网络安全等级保护4级", value: 4 },
  { label: "网络安全等级保护3级", value: 3 },
  { label: "网络安全等级保护2级", value: 2 },
  { label: "网络安全等级保护1级", value: 1 },
];

export default {
  name: "index",
  mixins: [BaseList],
  components: {
    Add,
    Detail,
  },
  data() {
    return {
      list: [],
      queryLevelList: [
        {
          label: "一级",
          value: 1,
        },
        {
          label: "二级",
          value: 2,
        },
        {
          label: "三级",
          value: 3,
        },
      ],
      total: 0,
      showSearch: true,
      loading: false,
      queryParams: {
        businessName: "",
        status: null,
        pageNum: 1,
        pageSize: 10,
        protectLevel: null,
      },
      form: {},
      ids: [],
      protectLevelList: protectLevelList,
    };
  },
  methods: {
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.$refs.queryForm.resetFields();
    },
    handleAdd() {
      this.$refs.systemAdd.showDialog();
    },
    handleDetail(row) {
      this.$refs.systemDetail.showDetail(row.id);
    },
    handleUpdate(row) {
      if (row.status === 0) {
        return this.$GlobalErrorConfirm("编辑失败", "启用状态下不允许编辑");
      }
      this.$refs.systemAdd.showDialog(row.id);
    },
    getList() {
      this.loading = true;
      list(this.queryParams)
        .then((res) => {
          if (res && res.code === 200) {
            this.list = res.rows.reduce((r, c) => {
              r.push({
                ...c,
                isDisabledForShake: false,
              });
              return r;
            }, []);
            this.total = res.total;
          } else {
            this.list = [];
            this.total = 0;
            this.handleListTips(res);
          }
        })
        .finally(() => (this.loading = false));
    },
    changeVal(val) {
      return this.queryLevelList.find((item) => item.value === val)?.label;
    },

    deleteData(row) {
      if (row && row.status === 0) {
        this.$GlobalErrorConfirm("警告", "启用状态不允许删除");
        return;
      }
      let interfaceSwitch = this.list.find(
        (v) => this.ids.includes(v.id) && v.status === 0
      );
      if (interfaceSwitch) {
        this.$GlobalErrorConfirm("删除失败", "启用状态不允许删除");
        return;
      }
      const ids = row?.id || this.ids.join(",");
      this.$GlobalDeleteConfirm()
        .then(() => {
          remove({ ids }).then((res) => {
            if (res && res.code === 200) {
              this.$GlobalSuccessMessage("删除成功");
              if (
                this.queryParams.pageNum > 1 &&
                ids.split(",").length === this.list.length
              ) {
                this.queryParams.pageNum -= 1;
              }
              this.getList();
            } else {
              this.$GlobalErrorConfirm("删除失败", res.msg);
            }
          });
        })
        .catch(() => {});
    },
    beforeDeleteForCheck(ids) {
      return new Promise((resolve, reject) => {
        resolve(ids);
      });
    },

    //  启用/停用
    changeStatus(row) {
      return new Promise((resolve, reject) => {
        let status = row.status === 0 ? 1 : 0;
        updateStatus({ status: status, id: row.id })
          .then((res) => {
            if (res && res.code === 200) {
              this.$GlobalSuccessMessage("操作成功");
              this.getList();
              resolve(res);
            } else {
              reject();
              this.$GlobalErrorConfirm("操作失败", res.msg).then();
            }
          })
          .catch(() => reject());
      });
    },

    importShow() {
      this.$refs.uploadDialog.show();
    },

    getProtectLevelLabel(val) {
      return protectLevelList.find((item) => item.value === val)?.label;
    },
  },
  mounted() {
    this.getList();
  },
};
</script>

<style scoped></style>
